package goodbaby.dklt.activity;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.util.Log;

import com.baidu.mobstat.StatService;
import com.classic.core.activity.BaseActivityStack;
import com.classic.core.activity.BaseSplashActivity;
import com.classic.core.utils.StringUtil;
import com.classic.core.utils.ToastUtil;
import com.classic.okhttp.gbb.http.ActionHelp;
import com.classic.okhttp.gbb.http.ObjectCallback;
import com.google.gson.reflect.TypeToken;

import java.lang.reflect.Type;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import cn.jpush.android.api.JPushInterface;
import cn.jpush.android.api.TagAliasCallback;
import goodbaby.dklt.GbbCache;
import goodbaby.dklt.GbbPreferences;
import goodbaby.dklt.R;
import goodbaby.dklt.bean.LoginInfo;
import goodbaby.dklt.bean.VersionInfo;

/**
 * Created by Jayden on 2016/4/19 16:36.
 *
 * @version V1.0
 * @Title: SplashActivity
 * @Description: 启动页
 */
public class SplashActivity extends BaseSplashActivity {

    private String account = "";
    private String isFirst = "y";
    private String isKeep = "n";
    private Class<?> nextActivity = LoginActivity.class;
    private String password = "";

    void checkLogin() {
        ActionHelp.userLogin(this, this.account, this.password, new ObjectCallback<LoginInfo>() {

            public void onError(int paramAnonymousInt) {
                ToastUtil.showToast(SplashActivity.this, "登陆失败");
                nextActivity = LoginActivity.class;
            }

            public void onError(int paramAnonymousInt, String paramAnonymousString) {
                super.onError(paramAnonymousInt, paramAnonymousString);
                ToastUtil.showToast(SplashActivity.this, "登陆失败");
                nextActivity = LoginActivity.class;
            }

            public void onHttpError(int paramAnonymousInt) {
                super.onHttpError(paramAnonymousInt);
                ToastUtil.showToast(SplashActivity.this, "网络异常");
                nextActivity = LoginActivity.class;
            }

            public void onSuccess(LoginInfo loginInfo) {
                checkVersion();
                GbbCache.setAccount(account);
                GbbCache.setPassword(password);
                GbbPreferences.saveUserAccount(account);
                GbbPreferences.saveUserPwd(password);
                GbbPreferences.saveSchoolName(loginInfo.getSchoolName());
                GbbPreferences.saveClassName(loginInfo.getClassName());
                GbbPreferences.saveClassId(StringUtil.getNumber(loginInfo.getClassId()));
                GbbPreferences.saveClassTeacherId(loginInfo.getClassTeacherID());
                GbbPreferences.saveHeight(loginInfo.getHeight());
                GbbPreferences.saveBirthday(loginInfo.getBirthday());
                GbbPreferences.saveName(loginInfo.getName());
                GbbPreferences.saveBraceletAddress(loginInfo.getBraceletAddress());
                GbbPreferences.saveAdminId(loginInfo.getAdminId());
                GbbPreferences.saveWeight(loginInfo.getWeight());
                GbbPreferences.saveSex(loginInfo.getSex());
                GbbPreferences.saveRole(loginInfo.getRoleId() + "");
                if (!StringUtil.isEmpty(loginInfo.getSObject()))
                    GbbPreferences.saveSObject(loginInfo.getSObject());
                if (!StringUtil.isEmpty(loginInfo.getImagePath()))
                    GbbPreferences.saveHeadImg(loginInfo.getImagePath());
                GbbPreferences.saveRole(loginInfo.getRoleId() + "");
                Set<String> tagSet = new LinkedHashSet<String>();
                tagSet.add(loginInfo.getClassId());
                tagSet.add(loginInfo.getSchoolId());
                JPushInterface.setAliasAndTags(getApplicationContext(), loginInfo.getAdminId(), tagSet, new TagAliasCallback() {
                    @Override
                    public void gotResult(int i, String s, Set<String> set) {
                        Log.e("JPushS", i + "");
                        Log.e("JPushA", s);
                        Log.e("JPushT", set.toString());
                    }
                });
                nextActivity = MainActivity.class;
            }

            @Override
            public Type getType() {
                return new TypeToken<LoginInfo>() {
                }.getType();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        JPushInterface.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        JPushInterface.onPause(this);
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }


    protected Class<?> nextActivity() {
        return nextActivity;
    }

    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        account = GbbPreferences.getUserAccount();
        password = GbbPreferences.getUserPwd();
        isKeep = GbbPreferences.getKeepLogin();
        if (isKeep != null && isKeep.equals("y")) {
            ToastUtil.showToast(SplashActivity.this, "正在登陆...");
            checkLogin();
        } else {
            checkVersion();
            nextActivity = LoginActivity.class;
        }
    }

    private void checkVersion() {
        ActionHelp.gbbVersion(this, new ObjectCallback<VersionInfo>() {
            @Override
            public void onError(int code) {
                ToastUtil.showToast(SplashActivity.this, "获取版本失败");
            }

            @Override
            public void onSuccess(VersionInfo response) {
                if (response != null) {
                    int newVersionCode = Integer.parseInt(response.getVERSIONNUM());
                    if (newVersionCode > getVersionCode()) {
                        GbbPreferences.saveVersion("y");
                        GbbPreferences.saveVersionDescrtption(response.getVERSIONDESCRIBLE());
                        GbbPreferences.saveVersionUrl(response.getPATH());
                    } else {
                        GbbPreferences.saveVersion("n");
                    }
                }
            }

            @Override
            public Type getType() {
                return new TypeToken<VersionInfo>() {
                }.getType();
            }
        });
    }

    /**
     * 获取版本号
     *
     * @return 当前应用的版本号
     */
    public int getVersionCode() {
        try {
            PackageManager manager = this.getPackageManager();
            PackageInfo info = manager.getPackageInfo(this.getPackageName(), 0);
            String version = info.versionName;
            int versionCode = info.versionCode;
            return versionCode;
        } catch (Exception e) {
            e.printStackTrace();
            return 0;
        }
    }

    protected void setSplashResources(List<BaseSplashActivity.SplashImgResource> paramList) {
        isFirst = GbbPreferences.getIsFirst();
        GbbPreferences.saveIsFirst("n");
        SplashImgResource sr = new SplashImgResource();
        sr.setmResId(R.mipmap.start);
        sr.setPlayerTime(2000);
        paramList.add(sr);
        if (isFirst == null) {
            SplashImgResource sr2 = new SplashImgResource();
            sr2.setmResId(R.mipmap.welcome_1);
            sr2.setPlayerTime(3000);
            paramList.add(sr2);
            SplashImgResource sr3 = new SplashImgResource();
            sr3.setmResId(R.mipmap.welcome_2);
            sr3.setPlayerTime(3000);
            paramList.add(sr3);
            SplashImgResource sr4 = new SplashImgResource();
            sr4.setmResId(R.mipmap.welcome_3);
            sr4.setPlayerTime(3000);
            paramList.add(sr4);
            SplashImgResource sr5 = new SplashImgResource();
            sr5.setmResId(R.mipmap.welcome_4);
            sr5.setPlayerTime(3000);
            paramList.add(sr5);
        }
    }
}
